package com.nantian.common.io;

import java.io.IOException;
import java.io.InputStream;

/**
 * <p>ClassName: IoUtils</p >
 * <p>Description: </p >
 * <p>Copyright: Copyright©2014</p >
 * <p>Company: 广州南天电脑系统有限公司</p >
 * <p>Date: 2021-08-27</p >
 *
 * @author 刘晓辉
 * @version 1.0
 */
public class IOUtils {

    /**
     * 读取流中指定长度的字节数据
     * @param inputStream 输入流
     * @param dataLength 指定长度
     * @return 字节数组
     * @throws IOException IO异常
     */
    public static byte[] read(InputStream inputStream, int dataLength) throws IOException {
        byte[] data = new byte[dataLength];
        int offset = 0;
        int len = dataLength;
        int tmpIndex;
        while (-1 != (tmpIndex = inputStream.read(data, offset, len)) ) {
            offset += tmpIndex;
            if (offset >= dataLength) {
                break;
            }
            len = dataLength - offset;
        }
        return data;
    }

}
